﻿using MyTask.Domain.Models;
using MyTask.Infrastructure.Context;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyTask.Domain.Queries.Handlers
{
   public class TaskInfoQueryHandler
    {
        #region Private variable
        //DbContext
        private readonly MyTaskContext _context;
        #endregion
        #region Constructor
        public TaskInfoQueryHandler(MyTaskContext context)
        {
            _context = context;
        }
        #endregion
        #region Public method
        /// <summary>
        /// Get tasks by user
        /// </summary>
        /// <param name="_query"></param>
        /// <returns></returns>
        public List<TaskInfo> GetTaskInfosByUser(GetTaskInfosByUserQuery _query)
        {
            var result = _context.TaskInfos.Where(p => p.UserId == _query.UserId).ToList();
            return result;
        }
        /// <summary>
        /// Get Task by taskId
        /// </summary>
        /// <param name="_query"></param>
        /// <returns></returns>
        public TaskInfo GetTaskInfoById(GetTaskInfoByIdQuery _query)
        {
            var result = _context.TaskInfos.FirstOrDefault(p => p.TaskId == _query.TaskId);
            return result;
        }
        public List<TaskInfo> GetSharedTasksByUser(GetSharedTasksByUserQuery _query)
        {
            var taskIds = _context.ShareTasks.Where(p => p.ToUserId == _query.UserId).Select(p => p.TaskInfoId).ToList();
            var result = _context.TaskInfos.Where(p => taskIds.Contains(p.TaskId)).ToList();
            return result;
        }

        #endregion
    }
}
